package com.ts.ischool.utils;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 时间工具类
 */
public class DateUtils {
	private static Log log = LogFactory.getLog(DateUtils.class);
	private static final String DATE_FORMAT_DEFAULT = "yyyy-MM-dd HH:mm:ss";
	
	/**
	 * @param datetime java.util.Date
	 * @return java.util.Date
	 */
	public static Date getFormateDate(Date datetime){
		return getFormateDate(datetime, DATE_FORMAT_DEFAULT );		
	}

	public static Date getFormateDate(Date datetime, String dateformat){
		Date date = null;
		try{
			java.text.DateFormat format = new java.text.SimpleDateFormat(dateformat);
			String mDateTime;			
			mDateTime = format.format(datetime);	
			date = getFormateDate(mDateTime,dateformat);
		}catch(Exception ex){
			log.error("getFormateDate执行失败："+ex.toString());	
		}
		return date;
		
	}
	
	/**
	 * 将时间字符串转换为指定的时间格式的日期类型
	 * @param datetime   java.util.Date
	 * @param dateformat 指定的时间格式
	 * @return java.util.Date 
	 */
	public static Date getFormateDate(String datetime, String dateformat){
		java.text.DateFormat format = new java.text.SimpleDateFormat(dateformat);
		Date date = null;
		try{
			date = format.parse(datetime);
		}catch(ParseException ex){
			log.error("getFormateDate执行失败："+ex.toString());	
		}
		
		return date;
	}
	
	public static Date getFormateDate(String datetime){
		return getFormateDate(datetime, DATE_FORMAT_DEFAULT);
	}
	
	/**
	 * 将时间字符串转换为指定的时间格式的时间字符串
	 * @param datetime   java.util.Date
	 * @param dateformat 指定的时间格式
	 * @return String
	 */
	public static String getFormateDateStr(Date datetime, String dateformat){
		String mDateTime = null;
		try{
			java.text.DateFormat format = new java.text.SimpleDateFormat(dateformat);
			if (datetime != null) {
				mDateTime = format.format(datetime);
			}
		}catch(Exception ex){
			log.error("getFormateDateStr执行失败："+ex.toString());	
		}
		return mDateTime;
		
	}

	/**
	 * 获取指定时间格式的时间
	 * @param dateformat
	 * @return
	 */	
	public static String getFormateDateStr(Date datetime){
		return getFormateDateStr(datetime, DATE_FORMAT_DEFAULT);
	}
	
	/**
	 * 获取指定时间格式的系统时间
	 * @param dateformat
	 * @return
	 */
	public static String getFormateSysDateStr(String dateformat){
		String mDateTime = null;
		try{
			Date datetime = new Date();
			java.text.DateFormat format = new java.text.SimpleDateFormat(dateformat);	
			mDateTime = format.format(datetime);	
		}catch(Exception ex){
			log.error("getFormateSysDateStr执行失败："+ex.toString());	
		}
		return mDateTime;
		
	}
	/**
	 * 获取默认时间格式的系统时间
	 * @return
	 */
	public static String getFormateSysDateStr(){
		return getFormateSysDateStr(DATE_FORMAT_DEFAULT);
	}
	
	public static Date getFormateSysDate(String dateformat){
		return getFormateDate(new Date(),dateformat);
		
	}
	
	public static Date getFormateSysDate(){
		 return getFormateSysDate(DATE_FORMAT_DEFAULT);
	}
	
	public static Date addHours(Date datetime, int i) {
		Calendar c = Calendar.getInstance();
		Date date = null;
		try{
			c.setTime(datetime);
			c.add(Calendar.HOUR_OF_DAY, i);
			date = c.getTime();
		}catch(Exception ex){
			log.error("addHours执行失败："+ex.toString());	
		}
		return date;
	}
	
	public static Date addDays(Date datetime, int i) {
		Calendar c = Calendar.getInstance();
		Date date = null;
		try{
			c.setTime(datetime);
			c.add(Calendar.DAY_OF_MONTH, i);
			date = c.getTime();
		}catch(Exception ex){
			log.error("addDays执行失败："+ex.toString());	
		}
		return date;
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
	}
	
	  /**  
     * begin date of this month：xxxx-yy-zz (eg: 2007-12-31)  
     * @return String  
     * @author pure  
     **/  
	public static String thisMonth() {   
		
		int x;                  // year 
		int y;                  // month
	    Calendar localTime = Calendar.getInstance();
		
        String strY = null;   
        x = localTime.get(Calendar.YEAR);   
        y = localTime.get(Calendar.MONTH) + 1;   
        strY = y >= 10 ? String.valueOf(y) : ("0" + y);return x + "-" + strY + "-01";   
    }   
    /**  
      * end date of this month：xxxx-yy-zz (eg: 2007-12-31)  
      * @return String  
      * @author pure  
      **/  
    public static String thisMonthEnd() {   
		int x;                  // year 
		int y;                  // month
	    Calendar localTime = Calendar.getInstance();
        String strY = null;   
        String strZ = null;   
        boolean leap = false;   
        x = localTime.get(Calendar.YEAR);   
        y = localTime.get(Calendar.MONTH) + 1;   
        if (y == 1 || y == 3 || y == 5 || y == 7 || y == 8 || y == 10 || y == 12) {   
            strZ = "31";   
        }   
        if (y == 4 || y == 6 || y == 9 || y == 11) {   
            strZ = "30";   
        }   
        if (y == 2) {   
            leap = leapYear(x);   
            if (leap) {   
                strZ = "29";   
            }else {   
                strZ = "28";   
            }   
        }   
        strY = y >= 10 ? String.valueOf(y) : ("0" + y);   
        return x + "-" + strY + "-" + strZ;   
    }   
    
    public static boolean leapYear(int year) {   
        boolean leap;   
        if (year % 4 == 0) {   
            if (year % 100 == 0) {   
                if (year % 400 == 0) leap = true;   
                    else leap = false;   
                }   
            else leap = true;   
        }   
        else leap = false;   
        return leap;   
    }   
}
